﻿using System;
using HiTeachers.Entities;
using HiTeachers.Services;
using HiTeachers.Utilities;

namespace HiTeachers.Web
{
    public partial class AdjustGuide : PageBase
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Page.IsPostBack) return;

            Page.CloseMessage(lblMsg);
            //var staticPage = StaticPageServices.GetRule();

            //if (staticPage == null) return;
            //hdfContentId.Value = staticPage.ID.ToString();
            //txtContent.Text = staticPage.Content;
        }

        protected void DdlContentTypeSelectedIndexChanged(object sender, EventArgs e)
        {
            Page.CloseMessage(lblMsg);
            if (string.IsNullOrEmpty(ddlContentType.SelectedValue))
            {
                ResetInputFields();
                return;
            }

            StaticPage staticPage;
            var contentType = (ContentType)Enum.Parse(typeof(ContentType), ddlContentType.SelectedValue);

            switch (contentType)
            {
                case Entities.ContentType.Rules:
                    {
                        staticPage = StaticPageServices.GetRule();
                        break;
                    }
                case Entities.ContentType.Intro:
                    {
                        staticPage = StaticPageServices.GetIntroduce();
                        break;
                    }
                default:
                    {
                        staticPage = null;
                        break;
                    }
            }

            if (staticPage == null)
                ResetInputFields();
            else
            {
                hdfContentId.Value = staticPage.ID.ToString();
                txtContent.Text = staticPage.Content;
            }
        }

        protected void BtnSaveClick(object sender, EventArgs e)
        {
            if (!Page.IsValid) return;
            if (string.IsNullOrEmpty(ddlContentType.SelectedValue)) return;

            try
            {
                StaticPage staticPage;
                var contentType = (ContentType)Enum.Parse(typeof(ContentType), ddlContentType.SelectedValue);
                if (string.IsNullOrEmpty(hdfContentId.Value))
                {
                    staticPage = new StaticPage
                                        {
                                            Content = txtContent.Text,
                                            ContentType = contentType,
                                            SortIndex = 0,
                                            Title = string.Empty
                                        };
                }
                else
                {
                    staticPage = StaticPageServices.GetById(new Guid(hdfContentId.Value));
                    staticPage.Content = txtContent.Text;
                }

                StaticPageServices.SaveOrUpdate(staticPage);
                Page.DisplayMessage(lblMsg, "Cập nhật thành công!", MessageType.Success);
                btnCancel.Text = @"Đóng";
            }
            catch (Exception ex)
            {
                Logger.Debug(ex.Message);
                Page.DisplayMessage(lblMsg, "Cập nhật không thành công!", MessageType.Error);
            }
        }

        protected void BtnCancelClick(object sender, EventArgs e)
        {
            Page.CloseMessage(lblMsg);

            ResetInputFields();
            ddlContentType.SelectedValue = string.Empty;
        }

        #region Private Methods
        private void ResetInputFields()
        {
            hdfContentId.Value = string.Empty;
            txtContent.Text = string.Empty;
            btnCancel.Text = @"Hủy";
        }
        #endregion
    }
}
